 //
//  WBVisitorView.swift
//  WeiBoApp
//
//  Created by James on 2017/6/18.
//  Copyright © 2017年 James. All rights reserved.
//

import UIKit

class WBVisitorView: UIView {
    
    ///注册按钮 登录按钮
    lazy var registerButton:UIButton = UIButton.wh_button(withTitle: "注册", back: UIColor.clear, backImageName: "common_button_white_disable",selectImageName:"", titleColor: UIColor.orange, fontSize: 15, frame: CGRect(x: 100, y: 100, width: 100, height: 30), cornerRadius:0.0)
    
    lazy var loginButton:UIButton = UIButton.wh_button(withTitle: "登录", back: UIColor.clear, backImageName: "common_button_white_disable",selectImageName:"", titleColor: UIColor.gray, fontSize: 15, frame: CGRect(x: 100, y: 100, width: 100, height: 30), cornerRadius:0.0)
    
    // MARK: -设置访客试图信息
    
    /// 使用字典设置访客试图的信息
    /// - Parameter dict: [imageName/message]
    var visitorInfo : [String:String]? {
        didSet{
            guard let imageNmae = visitorInfo?["imageName"] ,
                let message = visitorInfo?["message"]  else{
                    return
            }
            //1>设置消息
            tipLabel.text = message
            //如果是主页，图片名传入 "" 不需要设置图片
            if imageNmae == ""{
                startAnimate()
                return
            }
            //2>设置图片
            iconView.image = UIImage(named: imageNmae)
            houseView.isHidden = true
            maskIconView.isHidden = true
        }
    }
    
    // MARK: -旋转图标动画
    fileprivate func startAnimate(){
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2*M_PI
        animation.duration = 15
        animation.repeatCount = MAXFLOAT
        //动画完成不删除，如果view被释放，动画会停
        animation.isRemovedOnCompletion = false
        //将动画添加到图层
        iconView.layer.add(animation, forKey: nil);
    }
    
    // MARK: -访客试图
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
   // MARK: -私有控件
    //图像试图
    fileprivate lazy var iconView = UIImageView(image:#imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    
    //小房子
    fileprivate lazy var houseView = UIImageView(image:UIImage(named: "visitordiscover_feed_image_house"))
    //提示标签
    fileprivate lazy var tipLabel:UILabel = UILabel.wh_label(
        withText: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜",
                                                             textFont: 14,
                                                             textColor: UIColor.gray,
                                                             frame: CGRect(x: 100, y: 100, width: 100, height: 30))
    ///遮罩图像
    fileprivate lazy var maskIconView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))

    
}

// MARK: -设置界面
extension WBVisitorView{
    func setUI()  {
         backgroundColor = UIColor.wh_color(withHex: 0xEDEDED)
        //1.添加控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(tipLabel)
        addSubview(houseView)
        
        tipLabel.sizeToFit()
        tipLabel.numberOfLines = 0
        addSubview(registerButton)
        addSubview(loginButton)
        
        //2.取消autoresizing
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        let margin:CGFloat = 20.0
        
        
        //3.自动布局
        //1>图像试图
        addConstraint(NSLayoutConstraint(
            item: iconView,
            attribute: .centerX,
            relatedBy: .equal,
            toItem: self,
            attribute: .centerX,
            multiplier: 1.0,
            constant: 0))
        
        addConstraint(NSLayoutConstraint(
            item: iconView,
            attribute: .centerY,
            relatedBy: .equal,
            toItem: self,
            attribute: .centerY,
            multiplier: 1.0,
            constant: -40))
        
        //2>小房子
        addConstraint(NSLayoutConstraint(
            item: houseView,
            attribute: .centerX,
            relatedBy: .equal,
            toItem: iconView,
            attribute: .centerX,
            multiplier: 1.0,
            constant: 0))
        
        addConstraint(NSLayoutConstraint(
            item: houseView,
            attribute: .centerY,
            relatedBy: .equal,
            toItem: iconView,
            attribute: .centerY,
            multiplier: 1.0,
            constant: 0))
        //3>提示标签
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: iconView,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: margin))
        
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 236))
        
        //4>注册按钮
        addConstraint(NSLayoutConstraint(item: registerButton,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: margin))
        
        addConstraint(NSLayoutConstraint(item: registerButton,
                                         attribute: .left,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .left,
                                         multiplier: 1.0,
                                         constant: 0))
        
        addConstraint(NSLayoutConstraint(item: registerButton,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 100))
    
        //5>注册按钮
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: margin))
        
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .right,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .right,
                                         multiplier: 1.0,
                                         constant: 0))
        
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: registerButton,
                                         attribute: .width,
                                         multiplier: 1.0,
                                         constant: 0))
        //6>遮罩图像约束 (使用VFL方法)
        //views: 定义VFL中控件的名称和实际名称映射关系
        //metrics:定义VFL中 () 的常数映射关系
        let viewDict = ["maskIconView":maskIconView,
                        "registerButton":registerButton
                        ] as [String : Any]
        let metrics = ["space":-20]
        

        addConstraints(NSLayoutConstraint.constraints(
            withVisualFormat: "H:|-0-[maskIconView]-0-|",
            options: [],
            metrics: nil,
            views: viewDict))
        
        addConstraints(NSLayoutConstraint.constraints(
            withVisualFormat: "V:|-0-[maskIconView]-(space)-[registerButton]",
            options: [],
            metrics: metrics,
            views: viewDict))


    }
    
    
}
